/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.settings;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import de.fuberlin.kosmik.R;

/**
 * Helper class for reading the settings.
 * 
 * @author Michael Schmidt
 */
public class Settings {
	/**
	 * Default value, if OSM layer should be visible.
	 */
	public static final boolean DEFAULT_SHOW_OSM_LAYER = false;

	/**
	 * Default value, how often to request GPS fix in seconds.
	 */
	public static final int DEFAULT_GPS_INTERVAL = 1;

	/**
	 * Default value, if track filter should be used.
	 */
	public static final boolean DEFAULT_TRACK_FILTER_ON = false;

	/**
	 * Default value, if smoothing filter should be used.
	 */
	public static final boolean DEFAULT_SMOOTHING_FILTER = false;

	private SharedPreferences prefs;
	private Resources res;

	/**
	 * Reading preferences.
	 * 
	 * @param context context to open preferences
	 */
	public Settings(Context context) {
		prefs = PreferenceManager.getDefaultSharedPreferences(context);
		res = context.getResources();
	}

	/**
	 * Show OSM layer in the map.
	 * 
	 * @return true if layer should be visible, else false
	 */
	public boolean showOsmLayer() {
		return prefs.getBoolean(res.getString(R.string.settings_key_osm_layer),
				DEFAULT_SHOW_OSM_LAYER);
	}

	/**
	 * Filter track coordinates.
	 * 
	 * @return true if track filter should be used, else false
	 */
	public boolean useTrackFilter() {
		return prefs.getBoolean(res.getString(R.string.settings_key_filter_btn),
				DEFAULT_TRACK_FILTER_ON);
	}

	/**
	 * Setting for using the smoothing filter.
	 * 
	 * @return true if filter should be used, else false
	 */
	public boolean useSmoothingFilter() {
		return prefs.getBoolean(res.getString(R.string.settings_key_smooth_filter),
				DEFAULT_SMOOTHING_FILTER);
	}

	/**
	 * Seconds between GPS requests.
	 * 
	 * @return time in seconds
	 */
	public final int gpsInterval() {
		try {
			return Integer.valueOf(
					prefs.getString(res.getString(R.string.settings_key_gps_interval),
							String.valueOf(DEFAULT_GPS_INTERVAL))).intValue();
		} catch (NumberFormatException nfe) {
			return DEFAULT_GPS_INTERVAL;
		}
	}

	/**
	 * Get the OSM account's username.
	 * 
	 * @return the username
	 */
	public final String osmUser() {
		return prefs.getString(res.getString(R.string.settings_key_username), "");
	}

	/**
	 * Get the OSM account's password.
	 * 
	 * @return the password.
	 */
	public final String osmPass() {
		return prefs.getString(res.getString(R.string.settings_key_username), "");
	}

}
